﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace ajayumi.develop.win32.Transport.AppUpgrade.Controls
{
    /// <summary>
    /// 更新结束
    /// Author   : 黄乙冬
    /// Date     : 2011-9-1 08:45:00
    /// </summary>
    public partial class UcFinish : UcBase
    {
        public UpgradeResultType UpgradeResult { get; set; }

        public UcFinish()
        {
            InitializeComponent();
        }

        public UcFinish(UcBase preControl)
            : base(preControl)
        {
            InitializeComponent();
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.SetButtonVisible(WizardFormButtons.Exit);

            switch (this.UpgradeResult)
            {
                case UpgradeResultType.Failure:
                    this.SetFinishText("您的软件在升级过程中发生错误，\r\n请联系技术人员解决。");
                    this.HideChkRunApp();
                    break;
                case UpgradeResultType.Success:
                    this.SetFinishText("您的软件已经成功升级为最新版本。");
                    break;
                case UpgradeResultType.Cancel:
                    this.SetFinishText("升级操作已取消。");
                    this.HideChkRunApp();
                    break;
                default:
                    break;
            }

            if (ArgumentSerialization.IsSilent)
            {
                Application.Exit();
            }
        }

        protected override void InitDefaultValue()
        {
            base.InitDefaultValue();

            this.Title = "升级结束";
        }

        protected virtual void SetFinishText(string text)
        { this.label1.Text = text; }

        /// <summary>
        /// 隐藏"运行程序"复选框
        /// </summary>
        protected virtual void HideChkRunApp()
        {
            this.chkRunApp.Checked = false;
            this.chkRunApp.Visible = false;
        }

        internal override void AppExit()
        {
            Debug.WriteLine("执行 AppExit 方法");
            if (this.chkRunApp.Checked)
            {
                string path = Path.Combine(ArgumentSerialization.AppDirectory, string.Concat(ArgumentSerialization.ProcessName, ".exe"));
                if (File.Exists(path))
                {
                    Process.Start(path);
                }
            }
        }
    }
}
